﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.SymptomHistory
{
    public class HistoryEdit : PatientBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected SymptomInfo SymData;
        protected SymptomHistoryInfo SymHistoryData;

        protected new void HandleCommon()
        {
            this.AddLocation("症状记录", "/SymptomHistory/Index.aspx");
            this.AddLocation("修改症状信息", "/SymptomHistory/HistoryEdit.aspx");
            this.SetTitle("修改症状信息");

            int id = RequestTools.GetInt("id", 0);
            if (id <= 0)
            {
                this.AddErrLine("请选择症状历史信息");
                return;
            }


            SymHistoryData = PatientSymptomBLL.Instance.GetSymptomHistory(PrData.Id, id);

            if (SymHistoryData.Id <= 0)
            {
                this.AddErrLine("错误的参数");
                return;
            }

            SymData = SymptomBLL.Instance.GetSymptomInfo(SymHistoryData.SymptomId);
            if (SymData.Id <= 0)
            {
                this.AddErrLine("错误的参数");
                return;
            }
        }

        protected new void HandlePost()
        {
            SymptomHistoryInfo info = this.ValidateForm();
            if (info == null) return;
            bool succ = true;
            succ = PatientSymptomBLL.Instance.UpdateSymptomHistory(info);
            if (succ)
            {
                this.AddMsgLine("修改成功");
            }
            else
            {
                this.AddErrLine("修改失败");
            }
            TempUrl = "/SymptomHistory/List.aspx?symid=" + info.SymptomId;
            this.SetUrl(TempUrl);
            this.SetMetaRefresh();
        }

        protected new void HandleGet()
        {
            this.AddDatePicker();
        }

        protected SymptomHistoryInfo ValidateForm()
        {
            SymptomHistoryInfo info = new SymptomHistoryInfo();

            info.PRId = PrData.Id;
            info.DegreeValue = RequestTools.GetInt("degree", -1);
            info.Id = RequestTools.GetInt("id", -1);
            if (info.Id <= 0)
            {
                this.AddErrLine("无效的参数");
                return null;
            }

            info.DiseaseId = PrData.DiseaseId;
            if (info.DegreeValue != Constants.SYMPTOM_SEVERE
                && info.DegreeValue != Constants.SYMPTOM_MODERATE
                && info.DegreeValue != Constants.SYMPTOM_MILD
                && info.DegreeValue != Constants.SYMPTOM_NONE
                )
            {
                this.AddErrLine("无效的症状程度 " + info.DegreeValue.ToString());
                return null;
            }

            // 对时间进行判断
            string value = RequestTools.GetFormString("date");
            if (value == "")
            {
                this.AddErrLine("请填写时间");
                return null;
            }

            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + value);
                return null;
            }

            if (DateTime.Compare(date, DateTime.Today) > 0)
            {
                this.AddErrLine("记录日期 " + date.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            //else if (DateTime.Compare(date, PrData.StartDate) < 0)
            //{
            //    this.AddErrLine("记录日期 " + date.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
            //    return null;
            //}

            //需要对判断是否有相同时间的记录（如果已经修改过时间）
            if (SymHistoryData.RecordDate.CompareTo(date) != 0 && PatientSymptomBLL.Instance.CheckSymptomHistory(PrData.Id, SymData.Id, date))
            {
                //提示是否需要覆盖历史记录
                this.AddErrLine("是否需要覆盖" + value + "已有的历史记录？");
                return null;
            }

            info.RecordDate = date;
            info.UpdateDate = DateTime.Now;
            info.SymptomId = SymData.Id;
            info.SurveyId = 0;

            return info;

        }
    }
}